package com.store.framework.db;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.rowset.CachedRowSet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.store.framework.dao.IAppRootDao;
import com.store.framework.entry.AppRoot;

public class AppRootDao implements IAppRootDao {
	private static Log LOG = LogFactory.getLog(AppRootDao.class);

	@Override
	public int Add(AppRoot ar) {
		int at = 0;
		String sql = "insert into app_root(Id,RootName,RootCode,Domain,Url,"
				+ "Param,Type,AddDate,addPer,EditDate,EditPer,DelDate,"
				+ "DelPer,Del,Remarks) value(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

		try {
			DaoUtil.executeUpdate(sql, ar.getId(), ar.getRootName(),
					ar.getRootCode(), ar.getDomain(), ar.getUrl(),
					ar.getParam(), ar.getType(), ar.getAddDate(),
					ar.getAddPer(), ar.getEditDate(), ar.getEditPer(),
					ar.getDelDate(), ar.getDelPer(), ar.getDel(),
					ar.getRemarks());
			at = 1;
		} catch (Exception e) {
			at = 0;
			LOG.error(e.getMessage(),e);
			throw new RuntimeException(e.getMessage(),e);
		}
		return at;
	}

	public int getCount() {
		int count = 0;
		String sql = "select count(0) c from app_root";
		try {
			CachedRowSet cr = DaoUtil.SimpleQuery(sql);
			if (cr.next()) {
				count = cr.getInt(1);
			}
		} catch (Exception e) {
			count = 0;
			LOG.error(e);
			throw new RuntimeException(e.getMessage(),e);
		}
		return count;
	}

	@Override
	public List<AppRoot> select(Long start, Long limit) {
		List<AppRoot> list = new ArrayList<AppRoot>();
		String sql = "select * from app_root limit ? ,?";
		try {
			CachedRowSet cr = DaoUtil.SimpleQuery(sql,start,limit);
			while (cr.next()) {
				AppRoot approot = new AppRoot();
				approot.setId(cr.getString("Id"));
				approot.setRootName(cr.getString("RootName"));
				approot.setRootCode(cr.getString("RootCode"));
				approot.setDomain(cr.getString("Domain"));
				approot.setUrl(cr.getString("Url"));
				approot.setParam(cr.getString("Param"));
				approot.setType(cr.getString("Type"));
				approot.setAddDate(cr.getDate("AddDate")==null?0:cr.getDate("AddDate").getTime());
				approot.setAddPer(cr.getString("AddPer"));
				approot.setEditDate(cr.getDate("EditDate")==null?0:cr.getDate("EditDate").getTime());
				approot.setEditPer(cr.getString("EditPer"));
				approot.setDelDate(cr.getDate("DelDate")==null?0:cr.getDate("DelDate").getTime());
				approot.setDelPer(cr.getString("DelPer"));
				approot.setDel(cr.getByte("Del"));
				approot.setRemarks(cr.getString("Remarks"));
				list.add(approot);
			}
		} catch (Exception e) {
			list = null;
			LOG.error(e.getMessage(),e);
			throw new RuntimeException(e.getMessage(),e);
		}
		return list;
	}
	public int delete(AppRoot app){
		int a=0;
		String sql = "delete from app_root where Id = ?";
		try {
			DaoUtil.executeUpdate(sql, app.getId());
			a=1;
		} catch (SQLException e) {
			a=0;
			LOG.error(e);
			throw new RuntimeException(e.getMessage(),e);
		}
		return a;
	}
	public int update(AppRoot app){
		int a=1;
		String sql = "update  app_root set delPer=?,delDate=?,param=?,"
				+ "rootName=?,"+ "remarks=? ,"+ "type=?, "+ "url=? ,"
				+ "editDate=?, "
				+ "addPer=?, "
				+ "domain=?, "
				+ "rootCode=?, "
				+ "del=? ,"
				+ "addDate=?, "
				+ "editPer=? "
				+ "where Id = ?";
		try {
			/* 
			 * "id":"1212",
			 */
			DaoUtil.executeUpdate(sql, 
					app.getDelPer(),
					app.getDelDate(),
					app.getParam(),
					app.getRootName(),
					app.getRemarks(),
					app.getType(),
					app.getUrl(),
					app.getEditDate(),
					app.getAddPer(),
					app.getDomain(),
					app.getRootCode(),
					app.getDel(),
					app.getAddDate(),
					app.getEditPer(),
					app.getId()
					);
			a=1;
		} catch (SQLException e) {
			a=0;
			LOG.error(e.getMessage(),e);
			throw new RuntimeException(e.getMessage(),e);
		}
		return a;
	}
	
}
